package com.song.goods.config.mybatis.typehandler;

import com.song.common.util.AESUtil;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SecretTypeHandler implements TypeHandler<String> {
    private final static String AES_KEY = "0abcdefggfedcba0";
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, String s, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, AESUtil.encrypt(s, AES_KEY));
    }

    @Override
    public String getResult(ResultSet resultSet, String s) throws SQLException {
        String encrypt = resultSet.getString(s);
        return AESUtil.decrypt(encrypt, AES_KEY);
    }

    @Override
    public String getResult(ResultSet resultSet, int i) throws SQLException {
        String encrypt = resultSet.getString(i);
        return AESUtil.decrypt(encrypt, AES_KEY);
    }

    @Override
    public String getResult(CallableStatement callableStatement, int i) throws SQLException {
        String encrypt = callableStatement.getString(i);
        return AESUtil.decrypt(encrypt, AES_KEY);
    }
}
